package com.itheima.ui;

import com.itheima.bean.User;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

public class LoginUI extends JFrame implements ActionListener {

    public static void main(String[] args) {
        EventQueue.invokeLater(() -> {
            LoginUI frame = new LoginUI();
            frame.setVisible(true);
        });
    }

    private JPanel mainPanel;                   // 主面板
    private JTextField loginNameField;           // 登录名输入框
    private JPasswordField passwordField;       // 密码输入框
    private JButton loginButton;                // 登录按钮
    private JButton registerButton;             // 注册按钮

    //定义一个静态集合，存储系统中的全部用户信息
    private static ArrayList<User> users = new ArrayList<>();

    // 初始化几个用户信息，用于测试
    static {
        users.add(new User("管理员", "admin", "123456"));
        users.add(new User("张三", "zhangsan", "zs1234"));
        users.add(new User("李四", "lisi", "ls666"));
    }


    public LoginUI() {

        this.setTitle("登录界面");
        this.setSize(500, 400);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setLocationRelativeTo(null); // 居中显示
        this.setResizable(false); // 不可调整大小

        // 初始化组件
        mainPanel = new JPanel(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.insets = new Insets(10, 10, 10, 10);

        JLabel titleLabel = new JLabel("黑马人事管理系统");
        titleLabel.setFont(new Font("宋体", Font.BOLD, 30)); // 增加字体大小
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.gridwidth = 2; // 跨两列
        gbc.anchor = GridBagConstraints.CENTER;
        mainPanel.add(titleLabel, gbc);

        JLabel usernameLabel = new JLabel("用户名:");
        usernameLabel.setFont(new Font("宋体", Font.PLAIN, 20)); // 增加字体大小
        gbc.gridx = 0;
        gbc.gridy = 1;
        gbc.gridwidth = 1; // 回到单列
        gbc.anchor = GridBagConstraints.EAST;
        mainPanel.add(usernameLabel, gbc);

        // 增加输入框高度和字体大小
        loginNameField = new JTextField(20);
        loginNameField.setFont(new Font("Arial", Font.PLAIN, 20));
        loginNameField.setPreferredSize(new Dimension(200, 30));
        gbc.gridx = 1;
        gbc.gridy = 1;
        gbc.anchor = GridBagConstraints.WEST;
        mainPanel.add(loginNameField, gbc);

        JLabel passwordLabel = new JLabel("密码:");
        passwordLabel.setFont(new Font("宋体", Font.PLAIN, 20)); // 增加字体大小
        gbc.gridx = 0;
        gbc.gridy = 2;
        gbc.anchor = GridBagConstraints.EAST;
        mainPanel.add(passwordLabel, gbc);

        // 增加输入框高度和字体大小
        passwordField = new JPasswordField(20);
        passwordField.setFont(new Font("Arial", Font.PLAIN, 20));
        passwordField.setPreferredSize(new Dimension(200, 30));
        gbc.gridx = 1;
        gbc.gridy = 2;
        gbc.anchor = GridBagConstraints.WEST;
        mainPanel.add(passwordField, gbc);

        // 增加按钮大小和字体大小，并居中显示
        loginButton = new JButton("登录");
        loginButton.setFont(new Font("宋体", Font.PLAIN, 20));
        loginButton.setPreferredSize(new Dimension(100, 30));
        gbc.gridx = 0;
        gbc.gridy = 3;
        gbc.gridwidth = 2; // 跨两列
        gbc.anchor = GridBagConstraints.CENTER;
        mainPanel.add(loginButton, gbc);

        // 增加按钮大小和字体大小，并居中显示
        registerButton = new JButton("注册");
        registerButton.setFont(new Font("宋体", Font.PLAIN, 20));
        registerButton.setPreferredSize(new Dimension(100, 30));
        gbc.gridx = 0;
        gbc.gridy = 4;
        gbc.gridwidth = 2; // 跨两列
        gbc.anchor = GridBagConstraints.CENTER;
        mainPanel.add(registerButton, gbc);

        // 设置按钮监听器
        /*loginButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String username = usernameField.getText();
                char[] passwordChars = passwordField.getPassword();
                String password = new String(passwordChars);
                System.out.println("登录: " + username + ", " + password);
            }
        });*/
        loginButton.addActionListener(this);

        /*registerButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JOptionPane.showMessageDialog(LoginFrame.this, "注册功能尚未实现");
            }
        });*/
        registerButton.addActionListener(this);

        // 设置背景颜色
        mainPanel.setBackground(Color.decode("#f0f0f0"));
        titleLabel.setForeground(Color.decode("#333333"));

        // 添加主面板到框架
        this.add(mainPanel);

        this.setVisible(true);       // 显示窗口
    }

    @Override
    public void actionPerformed(ActionEvent e) {

        //这里可能是登录按钮点击过来的，也可能是注册按钮点击过来的
        //先判断是哪个按钮点击过来的
        //Object btn = e.getSource();
        JButton btn = (JButton) e.getSource();      //强转
        if (btn == loginButton) {
            //System.out.println("登录按钮点击了");
            //功能独立成方法
            login();
        } else {
            //System.out.println("注册按钮点击了");

        }
    }

    private void login() {
        //1.获取用户输入
        String loginName = loginNameField.getText();
        char[] passwordChars = passwordField.getPassword();
        String password = new String(passwordChars);
        //2.判断用户输入是否为空
        int flag = 1;
        if (loginName.isEmpty() || password.isEmpty()) {
            JOptionPane.showMessageDialog(this, "用户名或密码不能为空");
            flag = 0;
        }
        //3.根据登录名称查询用户对象并返回，如果用户对象为空，则提示用户不存在，
        //否则继续判断密码是否正确，如果密码不正确，则提示密码错误，否则提示登录成功
        User user = getUserByLoginName(loginName);
        if (user == null && flag == 1) {
            JOptionPane.showMessageDialog(this, "登录名不存在！");

        }
        if (!user.getPassword().equals(password)) {
            JOptionPane.showMessageDialog(this, "密码错误！");

        } else {
            JOptionPane.showMessageDialog(this, "登录成功！");
            new EmployeeManageUI(user.getUsername());       //显示员工管理界面
            this.dispose();     //关闭当前登录界面
        }


    }

    //根据登录名称查询用户对象并返回
    private User getUserByLoginName(String loginName) {
        for (User user : users) {
            if (user.getLoginName().equals(loginName)) {
                return user;
            }
        }
        return null;        //用户不存在
    }
}